package com.example.productdataserviceprovider.service.baidu;

import com.example.productdataserviceprovider.service.AbstractService;
import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Deprecated
public class CitysServiceImpl extends AbstractService implements CitysService {
    @Override
    public Map<String, String> getCitys(String file) {
        Map<String,String> citys=new HashMap<>();
        BufferedReader reader=null;
        logger.info("start....");
        try {
            reader=new BufferedReader(new InputStreamReader(new FileInputStream(new File(file)),"GBK"));
            String line=null;
            while((line=reader.readLine())!=null){
                String[] datas=line.split("、");
                for(String data:datas){
                    int kuohao=data.indexOf("（");
                    if(kuohao!=-1){
                        data=data.substring(0,kuohao);
                    }
                    data=data.trim();
                    citys.put(data,data);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        logger.info("end...."+citys.size());
        return citys;
    }

    @Override
    public Map<String, String> getCitysLine(String file) {
        Map<String,String> librarys=new HashMap<>();
        BufferedReader reader=null;
        Pattern pattern1=Pattern.compile("(.*?)(省|市|区|州|县|旗|团|盟|综合)");
        try {
            reader=new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
            String line=null;
            int count=0;
            while((line=reader.readLine())!=null){
                if(!StringUtils.hasText(line))continue;
                count++;
                Matcher matcher=pattern1.matcher(line);
                if(matcher.find()){
                    String city=matcher.group(1);
                    if(StringUtils.hasText(city)){
                        librarys.put(city,"");
                    }
                    else{
                        logger.info("city is empty="+city+",line="+line);
                    }

                }else{
                    librarys.put(line,"");
                }
            }
            logger.info("count="+count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("totalSize="+librarys.size());
        return librarys;
    }
}
